﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FarseerPhysics.Controllers;
using FarseerPhysics.Dynamics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SpriteGame
{
    class GravityBall:Ball
    {
        double flipLife = (double)TimeSpan.FromSeconds(4.8).Ticks;
        double initLive = (double)TimeSpan.FromSeconds(3).Ticks;

        const float GRAVITYPOWER = 15;

        public GravityBall()
        {
        }

        public GravityBall(Scene _parent, float _width, float _height, BodyType _type, Vector2 _position, float _rotation, String _texture)
            :base(_parent, _width, _height, _type, _position, _rotation, _texture)
        {
        }

        GravityController gravity;

        public override void Update(GameTime _time)
        {
            //FarseerPhysics.DebugViewXNA.DebugViewXNA.Instance.DrawCircle(Position, gravity.MaxRadius, Color.Red);
            //FarseerPhysics.DebugViewXNA.DebugViewXNA.Instance.DrawCircle(Position, gravity.MinRadius, Color.Red);

            if (lifeTime > initLive && lifeTime < maxlife)
            {
                parent.LockPhysics();
                    gravity.Strength = GRAVITYPOWER;

                    if (gravity.MaxRadius > 10)
                    {
                        body.Mass = 500;
                        gravity.MaxRadius -= 0.1f;
                    }

                    if (lifeTime > flipLife)
                    {
                        gravity.Strength *= -2;
                    }
                parent.UnlockPhysics();
            }

            base.Update(_time);
        }

        public override void LoadContent(Scene _parent)
        {
            base.LoadContent(_parent);

            //body.Mass = 5.0f;

            parent.LockPhysics();
            gravity = new GravityController(0, 30, 0.1f);
            gravity.GravityType = GravityType.Linear;
            
            World.AddController(gravity);
            gravity.AddBody(body);
            parent.UnlockPhysics();
            isLoaded = true;
        }

        public override void Dispose()
        {
            world.RemoveController(gravity);
            base.Dispose();
        }
    }
}
